<template>
  <div>
    <el-form :model="dataForm" ref="dataForm" label-width="100px" size="small" :rules="rules">
      <el-form-item label="机构名称:" prop="corporationName">
        <el-input v-model="dataForm.corporationName" maxlength="8"
                  show-word-limit clearable></el-input>
      </el-form-item>

      <el-form-item label="机构图标" prop="imgUrl">
        <upload-single-img :value="dataForm.imgUrl" @onChange="onChange" tips="（请上传白底Logo，大小控制在3M以内）"/>
      </el-form-item>
      <el-form-item style="margin-top: 10px" label-width="130px">
        <el-button type="" @click="close">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          <fa-icon class="fa-check"/>
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {common} from '@/utils/common'
import uploadSingleImg from '@/components/uploadSingleImg'
import api from '../api'
import {getCookie} from '@/utils/util'
import {CONSTANT_OBJ} from '@/utils/constant'

export default {
  props: {editData: Object, isEdit: Boolean},
  components: {uploadSingleImg},
  data () {
    return {
      dataForm: {
        tenantId: getCookie('tenantId'),
        corporationId: '',
        corporationName: '',
        imgUrl: '',
      },
      rules: {
        corporationName: [{required: true, message: '请输入机构名称', trigger: 'blur'}],
        imgUrl: [{required: true, message: '请上传机构图片', trigger: 'blur'}]
      }
    }
  },
  methods: {

    onChange (file) {
      console.log('file', file)
      const {url, name} = file
      if (url) {
        this.dataForm.imgUrl = url
      }
    },

    /**
     * 提交代码
     */
    submit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            this.editUser()
          } else {
            this.addUser()
          }
        }
      })
    },
    // 新增
    addUser () {
      api.addCorporation(this.dataForm).then(res => {
        const {responseCode, responseMsg} = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess')
        }
      })
    },
    editUser () {
      api.editCorporation(this.dataForm).then(res => {
        const {responseCode, responseMsg} = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess')
        }
      })
    },
    close () {
      this.$emit('close')
    }
  },
  mounted () {
    console.log('1111111', this.editData)
    if (this.editData && this.editData.corporationId) {
      common.listAssign(this.dataForm, this.editData)
    }
  }
}
</script>

<style scoped>
</style>

